package com.panda.cn.utils.userAgent;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class UserAgentUtil{
    /**
     * 获取服务器ip地址
     *
     * @return
     */
    
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        // 获取到多个ip时取第一个作为客户端真实ip
        if (StrUtil.isNotEmpty(ip) && ip.contains(",")) {
            String[] ipArray = ip.split(",");
            if (ObjUtil.isNotEmpty(ipArray)) {
                ip = ipArray[0];
            }
        }
        return ip;
    }
    private static Searcher searcher;
    public static String getIpAddress(String ip){
        if ("127.0.0.1".equals(ip) || ip.startsWith("192.168")) {
            return "局域网IP";
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            return "无网络";
        }
        if (searcher == null) {
            try {
                File file = ResourceUtils.getFile("classpath:ipdb/ip2region.xdb");
                String dbPath = file.getPath();
                searcher = Searcher.newWithFileOnly(dbPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String region = null;
        String errorMessage = null;
        try {
            region = searcher.search(ip);
        } catch (Exception e) {
            errorMessage = e.getMessage();
            if (errorMessage != null && errorMessage.length() > 256) {
                errorMessage = errorMessage.substring(0,256);
            }
            e.printStackTrace();
        }
        // 输出 region
        System.out.println(region);
        return  region;
    }

    
    /**
     * 获取浏览器名称
     *
     * @return
     */
    public static String getBrowser(HttpServletRequest request) {
        try {
            final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
            Browser browser = userAgent.getBrowser();
            return browser.getName() + " " +userAgent.getBrowserVersion();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "unKnow";
    }
    
    /**
     * 获取浏览器名称
     *
     * @return
     */
    public static String getOs(HttpServletRequest request) {
        try {
            final UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
            OperatingSystem operatingSystem = userAgent.getOperatingSystem();
            return operatingSystem.getName();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "unKnow";
    }
    
    public static void main(String[] args) {
        String IP = "124.207.244.202";
        String address = getIpAddress(IP);
        System.out.println(address);
    }
}
